package com.quan.weixin.handle;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import me.chanjar.weixin.common.api.WxConsts.MenuButtonType;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage.Item;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Component
public class MenuHandler extends AbstractHandler {

	@Override
	public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService weixinService, WxSessionManager sessionManager) {

		System.out.println("======>自定义菜单事件"+wxMessage.getMsgType()+"==="+wxMessage.getEvent()+"=="+wxMessage.getEventKey());
		
		System.out.println(MenuButtonType.CLICK.equals(wxMessage.getEvent()));
		
		String msg = String.format("type:%s, event:%s, key:%s", wxMessage.getMsgType(), wxMessage.getEvent(), wxMessage.getEventKey());
		if (MenuButtonType.VIEW.equals(wxMessage.getEvent())) {
			return null;
		} else if(MenuButtonType.CLICK.equalsIgnoreCase(wxMessage.getEvent())) {
			System.out.println("菜单Click事件...");
			
			List<WxMpXmlOutNewsMessage.Item> articles = new ArrayList<WxMpXmlOutNewsMessage.Item>() ;
			Item item = new Item() ;
			item.setTitle("图文消息1");
			item.setDescription("图文消息描述");
			item.setPicUrl("https://www.baidu.com/img/bd_logo1.png?where=super");
			item.setUrl("http://www.badiu.com");
			
			Item item2 = new Item() ;
			item2.setTitle("图文消息2");
			item2.setDescription("图文消息描述2");
			item2.setPicUrl("https://www.baidu.com/img/bd_logo1.png?where=super");
			item2.setUrl("http://www.badiu.com");
			
			articles.add(item);
			articles.add(item2);
			return WxMpXmlOutMessage.NEWS().articles(articles).fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
			
		}

		return WxMpXmlOutMessage.TEXT().content(msg).fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
	}

}
